package com.tengfei.main.question.service.excel;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tengfei.common.excel.ExcelListener;
import com.tengfei.common.excel.ExcelResult;
import com.tengfei.common.exception.ServiceException;
import com.tengfei.common.utils.ValidatorUtils;
import com.tengfei.common.utils.spring.SpringUtils;
import com.tengfei.main.question.domain.Subjective;
import com.tengfei.main.question.domain.bo.SubjectiveBo;
import com.tengfei.main.question.domain.export.SubImportVo;
import com.tengfei.main.question.mapper.SubjectiveMapper;

import java.util.List;

/**
 * @author feige
 * @version v1.0
 * @date 2022-11-08-12:49
 * @description
 */
public class SubImportListener extends AnalysisEventListener<SubImportVo> implements ExcelListener<SubImportVo> {

    private final SubjectiveMapper subjectiveMapper;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public SubImportListener() {
        this.subjectiveMapper = SpringUtils.getBean(SubjectiveMapper.class);
    }

    @Override
    public void invoke(SubImportVo subImportVo, AnalysisContext analysisContext) {
        SubjectiveBo subjectiveBo = null;
        try {
            subjectiveBo = BeanUtil.copyProperties(subImportVo, SubjectiveBo.class);
            if (ObjectUtil.isNull(subjectiveBo.getSubGrand()) || subjectiveBo.getSubGrand() <= 0) {
                subjectiveBo.setSubGrand(Math.round((float) (Math.random() * 5) + 1));
            }
            ValidatorUtils.validate(subjectiveBo);
            Subjective subjective = BeanUtil.copyProperties(subjectiveBo, Subjective.class);
            subjectiveMapper.insert(subjective);
            successNum++;
            successMsg.append("<br/>").append(successNum).append("、客观题 ").append(subjectiveBo.getSubContent()).append(" 添加成功");

        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、客观题 " + subjectiveBo.getSubContent() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    @Override
    public ExcelResult<SubImportVo> getExcelResult() {
        return new ExcelResult<SubImportVo>() {
            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<SubImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
